package com.toman.Adpter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.toman.Entity.Work_Entity;
import com.toman.Interfaces.BaseUrl;
import com.toman.MyApplication;
import com.toman.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by  GridView_adpter on Administrator on 2017/11/3 on 16:13.
 */

public class Work_adpter extends BaseAdapter {

    //上下文对象
    Context context;
    //
    ArrayList<Work_Entity> arrayList;

    LayoutInflater inflater;

    MyApplication myApplication;

    //  LinearLayout mAd_bg;
    ImageView mdel_img;


    public Work_adpter(Context context, ArrayList<Work_Entity> arrayList, MyApplication myApplication) {
        this.context = context;
        this.arrayList = arrayList;
        this.myApplication = myApplication;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        AddPackage addPackage;
        // if (null == convertView){
        addPackage = new AddPackage();
        convertView = inflater.inflate(R.layout.work_item, null, false);
        //addPackage.img = (ImageView) convertView.findViewById(R.id.mWorkImg);
        addPackage.title = (TextView) convertView.findViewById(R.id.mWorkTitle);
        // mAd_bg = (LinearLayout) convertView.findViewById(R.id.mWork_bg);
        mdel_img = (ImageView) convertView.findViewById(R.id.mWork_Delimg1);

//            convertView.setTag(addPackage);
//        }else {
//            addPackage=(AddPackage) convertView.getTag();
//        }

        if (arrayList.get(position).isok()) {
            mdel_img.setVisibility(View.VISIBLE);
        } else {
            mdel_img.setVisibility(View.INVISIBLE);
        }

        // addPackage.img.setBackgroundResource(arrayList.get(position).getImg());
        addPackage.title.setText(arrayList.get(position).getTitle());

        mdel_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DelDialog(arrayList.get(position).getWorkshop_id(), position);

            }
        });


        return convertView;
    }


    public class AddPackage {
        ImageView img;
        TextView title;
    }


    /*
        提示删除
     */
    public void DelDialog(final Integer workshop_id, final int po) {
        final String token = myApplication.getSp().getString("tokenId", "");
        final AlertDialog dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        dialog.getWindow().setContentView(R.layout.del_layout2);
        dialog.getWindow().findViewById(R.id.mDeDimss2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        dialog.getWindow().findViewById(R.id.mDetermine2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OkHttpUtils
                        .post(BaseUrl.workshop_del)
                        .params("token", token)
                        .params("workshop_id", workshop_id.toString())
                        .execute(new StringCallback() {
                            @Override
                            public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                                try {
                                    JSONObject js = new JSONObject(result);
                                    int code = js.getInt("code");
                                    if (code == 1) {
                                        Toast.makeText(context, "" + js.getString("msg"), Toast.LENGTH_SHORT).show();
                                        arrayList.remove(po);
                                        notifyDataSetChanged();
                                        //workshop();
                                        dialog.dismiss();
                                    } else {
                                        Toast.makeText(context, "" + js.getString("msg"), Toast.LENGTH_SHORT).show();
                                        dialog.dismiss();
                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });

            }
        });

    }


    public void workshop() {
        String token = myApplication.getSp().getString("tokenId", "");
        OkHttpUtils
                .post(BaseUrl.workshop)
                .params("token", token)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        //Log.i("test", "result: "+result);
                        arrayList.clear();
                        try {
                            JSONObject js = new JSONObject(result);
                            int code = js.getInt("code");
                            if (code == 1) {
                                JSONObject data = js.getJSONObject("data");
                                JSONArray rows = data.getJSONArray("rows");
                                for (int i = 0; i < rows.length(); i++) {
                                    JSONObject ji = rows.getJSONObject(i);
                                    int id = ji.getInt("id");
                                    String name = ji.getString("workshop_name");
                                    Work_Entity entity = new Work_Entity(id, name, false);
                                    arrayList.add(entity);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }


}
